package com.kis.core.util;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by wongloong on 16-5-6.
 */
public class HeartBeat {

    public static boolean isAlive(String url) {
        boolean result = true;
        try {
            System.out.println(url);
            URL targetUrl = new URL(url);
            URLConnection connection = targetUrl.openConnection();
            HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
            httpURLConnection.setConnectTimeout(1500);
            httpURLConnection.setReadTimeout(1000);
            httpURLConnection.connect();
            int code = httpURLConnection.getResponseCode();
            if (!(code + "").startsWith("2")||!(code+"").startsWith("4")) {
                result=false;
                throw new RuntimeException(url + "服务暂时不可用");
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}
